package com.xhwy.service.impl;

import com.xhwy.entity.Address;
import com.xhwy.mapper.AddressMapper;
import com.xhwy.param.AddressParam;
import com.xhwy.param.AddressRemoveParam;
import com.xhwy.service.AddressService;
import com.xhwy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//地址业务实现类
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 根据用户id查询地址数据
     *  1. 直接进行查询数据库
     *  2. 结果进行封装即可
     * @param addressParam 已经校验完毕的查询参数
     * @return
     */
    @Override
    public R getAllByUserId(AddressParam addressParam) {
        List<Address> list = addressMapper.getAllByUserId(addressParam.getUserId());

        return R.ok("查询成功", list);
    }

    /**
     * 添加地址业务，添加成功后，返回新的数据集合
     *
     * @param address 地址数据已经校验完毕
     * @return
     */
    @Override
    public R add(Address address) {

        int rows = addressMapper.add(address);
        if (rows > 0){
//            return R.ok("插入地址成功");
            List<Address> list = addressMapper.getAllByUserId(address.getUserId());
            return R.ok("查询成功", list);
        }

        return R.fail("插入地址失败");
    }

    /**
     * 删除地址业务
     *
     * @param addressRemoveParam 地址id参数
     * @return
     */
    @Override
    public R delete(AddressRemoveParam addressRemoveParam) {

        int rows = addressMapper.delete(addressRemoveParam.getAddressId());
        if(rows > 0) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
}
